package andy.ham;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import andy.ham.Note;
import static andy.ham.R.drawable.back;
import static andy.ham.R.drawable.search;

public class searchDialy extends Activity {

    Button cancel;
    RecyclerView recyclerView;
    searchDiaryAdapter sdAdapter;
    // GreenDAO使用的变量
    private SQLiteDatabase db;
    private andy.ham.DaoMaster daoMaster;
    private andy.ham.DaoSession daoSession;
    private NoteDao ld;
    private Note login;
    private NoteDaoHelper ldh;
    private Long num;

    private EditText seatext;
    private String search_text;
    private List<Note> list;
    private Button edit_search;

    private Button error;
    //private Button search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_dialy);
        InitDAO_tag();
        initView();
        searchListener();
        error = (Button) findViewById(R.id.edit_cancel);

       error.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view1) {
                Intent intent1 = new Intent(searchDialy.this, LifeDiary.class);
                startActivity(intent1);
                finish();
            }
        });


    }


    private void InitDAO_tag() {
        andy.ham.DaoMaster.DevOpenHelper helper1 = new andy.ham.DaoMaster.DevOpenHelper(this, "notes-db", null);
        db = helper1.getWritableDatabase();
        daoMaster = new andy.ham.DaoMaster(db);//代表着一个数据库的连接
        daoSession = daoMaster.newSession();//可以让我们使用一些Entity的基本操作和获取Dao操作类
        ld = daoSession.getNoteDao();
        ldh = new NoteDaoHelper(ld);
//        list = new ArrayList<Note>();
    }
    private void initView() {
        seatext = (EditText) findViewById(R.id.search_text);
        edit_search=(Button) findViewById(R.id.edit_search);
    }
    public void searchListener(){
        edit_search.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                search_text=seatext.getText().toString();
                list=ldh.searchByKey(search_text);
               //list=ldh.searchByKey(search_text);
                recyclerView = (RecyclerView) findViewById(R.id.main_rcv);
                recyclerView.setLayoutManager(new LinearLayoutManager(searchDialy.this));//设置线性管理器
                ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
                sdAdapter = new searchDiaryAdapter(searchDialy.this, list);
                recyclerView.setAdapter(sdAdapter);
            }
        });

    }
}
